import axios from 'axios'
import { userProfileInfo, securitySettings, paymentMethods } from './data/profile'

// 创建一个模拟请求的函数
const createMockAPI = () => {
    // 模拟接口拦截器
    const mock = axios.create()

    // 请求拦截器
    mock.interceptors.request.use(config => {
        return config
    })

    // 响应拦截器
    mock.interceptors.response.use(response => {
        return response
    }, error => {
        // 模拟网络错误等情况
        return Promise.reject(error)
    })

    return mock
}

// 创建拦截器实例
const mockAPI = createMockAPI()

// 获取当前登录用户信息
export const getUserProfile = () => {
    return new Promise((resolve) => {
        // 从本地存储获取当前登录用户
        const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')

        // 如果没有登录用户，返回默认数据
        const data = Object.keys(userInfo).length > 0 ? userInfo : userProfileInfo

        setTimeout(() => {
            resolve({
                data,
                status: 200
            })
        }, 300)
    })
}

export const getSecuritySettings = () => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve({
                data: securitySettings,
                status: 200
            })
        }, 300)
    })
}

export const getPaymentMethods = () => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve({
                data: paymentMethods,
                status: 200
            })
        }, 300)
    })
}

// 模拟更新用户资料
export const updateUserProfile = (profileData) => {
    return new Promise((resolve) => {
        // 从本地存储获取当前登录用户
        const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
        const updatedUserInfo = { ...userInfo, ...profileData }

        // 更新本地存储
        localStorage.setItem('userInfo', JSON.stringify(updatedUserInfo))

        // 如果有userId，更新注册用户列表中的对应用户
        if (userInfo.userId) {
            const registeredUsers = JSON.parse(localStorage.getItem('registeredUsers') || '[]')
            const updatedUsers = registeredUsers.map(user => {
                if (user.userId === userInfo.userId) {
                    return { ...user, ...profileData }
                }
                return user
            })
            localStorage.setItem('registeredUsers', JSON.stringify(updatedUsers))
        }

        setTimeout(() => {
            resolve({
                data: updatedUserInfo,
                status: 200,
                message: "更新成功"
            })
        }, 500)
    })
}

// 模拟头像上传
export const uploadUserAvatar = (fileData) => {
    return new Promise((resolve) => {
        // 创建FileReader读取本地图片
        const reader = new FileReader()
        reader.onload = (e) => {
            // 使用本地Base64图片作为头像
            const imageDataUrl = e.target.result

            setTimeout(() => {
                resolve({
                    data: {
                        avatarUrl: imageDataUrl
                    },
                    status: 200,
                    message: "上传成功"
                })
            }, 500)
        }

        // 读取图片文件为DataURL (Base64)
        reader.readAsDataURL(fileData)
    })
}

// 模拟添加支付方式
export const addPaymentMethod = (paymentData) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve({
                data: {
                    id: "p00" + (paymentMethods.length + 1),
                    ...paymentData
                },
                status: 200,
                message: "添加成功"
            })
        }, 500)
    })
}

// 模拟更新安全设置
export const updateSecuritySetting = (settingData) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve({
                data: { ...securitySettings, ...settingData },
                status: 200,
                message: "设置更新成功"
            })
        }, 500)
    })
} 